package com.stedu.controller;

import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.service.AdminService;
import com.stedu.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*
* 获取已登录用户的信息并返回
* */
@CrossOrigin
@RestController
@RequestMapping("/admin/info")
public class InfoController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/top5")
    public RespBean top5DnameAndCount()  {
        List<Map<String, Object>> mapList = adminService.selectTop5DnameAndCount();
        return RespBean.ok("", mapList);
    }

    @GetMapping("/adminInfo")
    public RespBean getLoginInfo(HttpServletRequest request) {
        //获取jwt
        String token = request.getHeader("token");
        //解析jwt
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        //根据用户id查询信息
        Admin admin = adminService.selectById(id);
        //返回
        return RespBean.ok("", admin);
    }
    @GetMapping("/Rtop5")
    public RespBean selectTop5RnameAndCount()  {
        List<Map<String, Object>> mapList = adminService.selectTop5RnameAndCount();
        return RespBean.ok("", mapList);
    }
    @DeleteMapping("/{rid}")
    public RespBean deleteByRid(@PathVariable("rid") Integer rid) {
        try {
            adminService.deleteByRid(rid);
            return RespBean.ok("员工离职处理成功，关联关系已删除");
        } catch (Exception e) {
            return RespBean.error("员工离职处理失败：" + e.getMessage());
        }
    }
}
